package com.njd.test.entity;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jfinal.plugin.activerecord.Db;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 自动生成的Model
 * 
 * @author Johnson Zhang
 *
 */
@SuppressWarnings("serial")
public class Project extends Model<Project> {
	// --------------setter和getter---------------

	public Project setId(Integer id) {
		set("id", id);
		return this;
	}

	public Integer getId() {
		return get("id");
	}

	public Project setProjectName(String projectName) {
		set("project_name", projectName);
		return this;
	}

	public String getProjectName() {
		return get("project_name");
	}

	public Project setStatus(Integer status) {
		set("status", status);
		return this;
	}

	public Integer getStatus() {
		return get("status");
	}

	// -------------------------------------------
	/***
	 * 数据库操作实体
	 */
	public static final Project dao = new Project();
	/**
	 * 表名
	 */
	public static final String tableName = "project";
	/**
	 * 表主键
	 */
	public static final String primaryKey[] = TableMapping.me()
			.getTable(dao.getClass()).getPrimaryKey();

	/***
	 * 空构造
	 */
	public Project() {
	}

	/***
	 * 有参构造
	 */
	public Project(Integer id, String projectName, Integer status) {
		setData(id, projectName, status);
	}

	/***
	 * 数据批量设置
	 */
	public Project setData(Integer id, String projectName, Integer status) {
		set("id", id);
		set("project_name", projectName);
		set("status", status);
		return this;
	}

	/***
	 * 按ID逆序查询全部数据
	 * 
	 * @param pageNumber
	 *            页码
	 * @param pageSize
	 *            每页记录数
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> paginate(int pageNumber, int pageSize,
			String para, Object value) throws Exception {
		Object[] o = new Object[] {};
		StringBuffer sb = new StringBuffer();
		sb.append("from " + tableName);
		if (value != null && value != null && value != "") {
			o = new Object[] { value };
			sb.append(" where " + para + "=?");
		}
		sb.append(" order by " + primaryKey[0] + " DESC");
		Long num = Db.queryLong("select count(1) " + sb.toString(), o);
		List<?> data = paginate(pageNumber, pageSize, "select *",
				sb.toString(), o).getList();
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("data", data);
		m.put("num", num);
		return m;
	}

	public List<Project> getProjectList() {
		return find("SELECT * FROM " + tableName + " WHERE status = 0");
	}
	
	
	public List<Project> getProjectAndModuleList(int projectId) {
		List<Project> list = find("SELECT * FROM " + tableName + " WHERE status = 0" + (projectId > 0 ? " AND id = " + projectId : ""));
		
		for (Project p : list) {
			p.put("moduleList", Module.dao.getModuleListByProjectId(p.getId()));
		}
		
		return list;
	}
	
}